package demo.demo07.test02;

public class testStop implements Runnable {

    //1.设置标志位
    private boolean flag = true;

    @Override
    public void run() {
        int i = 0;
        while (flag) {
            System.out.println("线程运行" + i++);
        }
    }

    //2.设置stop方法停止线程
    public void stop() {
        this.flag = false;
    }

    public static void main(String[] args) {
        testStop testStop = new testStop();
        new Thread(testStop).start();

        for (int i = 0; i < 1000; i++) {

            System.out.println("主线程停止了");
            if (i == 900) {
                //切换标志位让线程停止
                testStop.stop();
                System.out.println("次线程停止了");
            }
        }
    }
}
